import {
	authType
} from "../../common/config.js";

/**
 * 请求拦截
 * @param {Object} http
 */
module.exports = (vm) => {
	uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
			// 初始化请求拦截器时，会执行此方法，此时data为undefined，赋予默认{}
			config.data = config.data || {}
			// 可以在此通过vm引用vuex中的变量，具体值在vm.$store.state中
			// config.header.Authorization = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhTWFzVHtbS2labUZEa1YiLCJhdWQiOiIiLCJpYXQiOjE3MTk0NjA3MzQsIm5iZiI6MTcxOTQ2MDczNCwiZXhwIjoxNzIwMzQ3MTM0LCJkYXRhIjp7Im1lbWJlcl9pZCI6IjAwMDAyIiwib3JnX2lkIjpudWxsLCJtZW1iZXJfbmFtZSI6Ilx1NWY5MFx1OGY4OSIsIm1lbWJlcl9zZXgiOm51bGwsIm1lbWJlcl9waG9uZSI6bnVsbCwibWVtYmVyX3R5cGUiOiJcdTY3MmNcdTY4MjFcdTViNjZcdTc1MWYiLCJtZW1iZXJfcGhvdG8iOm51bGwsInByb2plY3RfaWQiOm51bGx9fQ.PcFqp0SNhwK9H5z_bnIRC1HzIjM5rogIIgeWqmylrdY';
			// config.header.Authorization = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhTWFzVHtbS2labUZEa1YiLCJhdWQiOiIiLCJpYXQiOjE3MzEzMTI1OTgsIm5iZiI6MTczMTMxMjU5OCwiZXhwIjoxNzMyMTk4OTk4LCJkYXRhIjp7Im1lbWJlcl9pZCI6IjAwMDAxIiwib3JnX2lkIjpudWxsLCJtZW1iZXJfbmFtZSI6Ilx1NmJkYlx1NTM0ZVx1NWNiMyIsIm1lbWJlcl9zZXgiOm51bGwsIm1lbWJlcl9waG9uZSI6bnVsbCwibWVtYmVyX3R5cGUiOiJcdTY3MmNcdTY4MjFcdTViNjZcdTc1MWYiLCJtZW1iZXJfcGhvdG8iOm51bGwsInByb2plY3RfaWQiOjEwMDcyfX0.2yd-csRPp9H3h3WBz6Xbpv_rgNqAf_7MwwWzE2LeRhA';
			if (vm.$token) { // 授权方式
				if (authType == 'Bearer') {
					config.header.Authorization = 'Bearer ' + vm.$token;
				} else if (authType == 'jwtToken') {
					config.header.jwtToken = token;
				} else {
					config.header.Authorization = vm.$token;
				}
			}
			return config
		}, (config) => // 可使用async await 做异步操作
		Promise.reject(config))
}
